package com.yanqingying.controller;
import com.yanqingying.HuaWeiOBSProperties;
import com.yanqingying.Result;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

@RestController

public class UpController {
    @Autowired
    HuaWeiOBSProperties huaWeiOBSProperties;
    @PostMapping("/upload")
    public Result upLoad( MultipartFile file) throws IOException {
        // 您可以登录访问管理控制台获取访问密钥AK/SK
        String ak = "HPUAZKTTAN8CIODEFTAO";
        String sk = "4kM2Re9gFPrISZeEfKfqjVyrF4VyCcxDRR3IoGxa";
        ObsClient obsClient = new ObsClient(ak, sk,huaWeiOBSProperties.getEndpoint());
        String originalFilename = file.getOriginalFilename();
        String NewFileName = UUID.randomUUID()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        PutObjectResult result = obsClient.putObject(huaWeiOBSProperties.getBucketName(), NewFileName, file.getInputStream());
        return Result.success(result);
    }

}
